package com.huicoo.forestmanager.ui.patrol.PatrolRecordChildFragment;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.huicoo.forestmanager.R;
import com.huicoo.forestmanager.adapter.PatrolRecordChildAdapter;
import com.huicoo.forestmanager.base.BaseFragment;
import com.huicoo.forestmanager.network.bean.patrol.RecordlistBean;
import com.huicoo.forestmanager.ui.patrol.PatrolRecordChildFragment.contract.PatrolRecordChildFragmentContract;
import com.huicoo.forestmanager.ui.patrol.PatrolRecordChildFragment.presenter.PatrolRecordChildFragmentPresenter;
import com.huicoo.forestmanager.ui.patrol.PatrolRecordDetailsActivity.PatrolRecordDetailsActivity;
import com.huicoo.forestmanager.widget.LoadingDialog;
import com.liaoinstan.springview.container.DefaultFooter;
import com.liaoinstan.springview.container.DefaultHeader;
import com.liaoinstan.springview.widget.SpringView;

import butterknife.BindView;

/**
 * --------- 日期 ------- 维护人 ------------ 变更内容 --------
 * 2019/4/5		    Administrator
 * <p>
 * 巡查记录子片段
 */
public class PatrolRecordChildFragment extends BaseFragment implements PatrolRecordChildFragmentContract.View{
    @BindView(R.id.recycler_iew)
    RecyclerView recycler_iew;
//    @BindView(R.id.spring_view)
    SpringView spring_view;
    LoadingDialog loadingDialog;

    private PatrolRecordChildAdapter mForestCampAdapter;

    public static PatrolRecordChildFragment getInstance() {
        return new PatrolRecordChildFragment();
    }


    @Override
    protected int getLayoutId() {
        return R.layout.fragment_patrol_record_child;
    }

    @Override
    protected void initView() {
//        initSpringView();
        initRecyclerView();

        initData();
    }

    private void initData() {
        String string = getArguments().getString("Value");
        // 初始化P层
        presenter = new PatrolRecordChildFragmentPresenter(this);
        ((PatrolRecordChildFragmentPresenter) presenter).getrecordlist(string);
        if (loadingDialog == null) {
            loadingDialog = new LoadingDialog(getActivity());
            loadingDialog.show();
        }


    }


    private void initSpringView() {
        spring_view.setHeader(new DefaultHeader(getContext()));
        spring_view.setFooter(new DefaultFooter(getContext()));
        spring_view.setListener(new SpringView.OnFreshListener() {
            @Override
            public void onRefresh() {
                spring_view.onFinishFreshAndLoad(); //完成刷新和加载
            }
            @Override
            public void onLoadmore() {
                spring_view.onFinishFreshAndLoad(); //完成刷新和加载
            }
        });
    }

    private void initRecyclerView() {
        //设置布局管理器
        recycler_iew.setLayoutManager(new LinearLayoutManager(getContext()));
        //创建适配器
        mForestCampAdapter = new PatrolRecordChildAdapter(null);
        //设置适配器
        recycler_iew.setAdapter(mForestCampAdapter);
        //设置点击事件
        mForestCampAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                //巡查记录详情
                PatrolRecordDetailsActivity.start(getActivity(),mForestCampAdapter.getData().get(position));
            }
        });

    }




    @Override
    public void getrecordlistSuccess(RecordlistBean bean) {
        loadingDialog.dismiss();
        mForestCampAdapter.setNewData(bean.getData());
    }

    @Override
    public void getrecordlistFail(String msg) {
        loadingDialog.dismiss();
    }
}
